<?php


namespace app\model;

use TencentCloud\Csip\V20221121\Models\DbAssetInfo;
use think\model;
use think\facade\Db;
use app\model\Attachment as AttachmentModel;
use app\model\RemarkModel;
class DriverModel extends Model
{
    protected $table = "store_driver";

    /**
     * @param $id
     * @return DriverModel|array|mixed|Model|null
     * @notes 详情
     * @author: dongdong
     * @date: 2025/1/17 15:33
     */
    public function info($id,$type){
        try {
            if($type==1){
                return self::where(["id"=>$id])->find();
            }else{
                return self::where(["user_id"=>$id])->find();
            }
        }catch (\Exception $e){
            return [];
        }
    }


    /**
     * @param $param
     * @return DriverModel[]|array|\think\Collection
     * @notes 列表
     * @author: dongdong
     * @date: 2025/1/18 10:32
     */
    public function staff_lists($param){

        try {
            $carModel = new CarModel();

            $limit = $param['limit']??10;
            $page = $param['page']??1;
            $page = ($page-1)*$limit;

            $where = [];

            $state = $param['columnsEmplState_id']??'';
            if($state){
                $where['state'] = $state;
            }

            $type = $param['columnsEmplType_id']??'';
            if($type){
                $where['type'] = $type;
            }

            $no_user = $param['no_user']??0;

            $find = self::where(["is_deleted"=>0])->where(['user_id'=>$param['user_id'],'register_id'=>$param['user_id']])->find();
            if(empty($find)){
                $member = Db::connect("fast_mysql")->table("fa_user")->where(['id'=>$param['user_id']])->field("id,password,salt,user_password,mobile,username,username_ru")->find();

                $d['user_id'] = $param['user_id'];
                $d['register_id'] = $param['user_id'];
                $d['bind_user_id'] = $param['user_id'];

                $d['title'] = $member['username'];
                $d['title_ru'] = $member['username_ru'];
                $d['create_at'] = date('Y-m-d H:i:s');
                $d['phone'] = $member['mobile'];
                $d['type'] = 31;
                $d['password'] = $member['user_password'];
                $d['state'] = 1;
                $d['user_password'] = $member['password'];
                $d['salt'] = $member['salt'];

                self::insert($d);
            }

            $Attachment = new AttachmentModel();
            $Config = new ConfigModel();

            if($no_user==0){
                $lists = self::where(["is_deleted"=>0])->where(['user_id'=>$param['user_id']])->where($where)->order("id desc")->limit($page,$limit)->select();
            }else{
                $user_id = $param['user_id'];
                $lists = self::where(["is_deleted"=>0])->where(['user_id'=>$param['user_id']])->where("register_id!=$user_id")->where($where)->order("id desc")->limit($page,$limit)->select();
            }

            foreach ($lists as $k =>&$v){
                $v['attachment'] = $Attachment->field("id,path")->where("id","in",$v['attachment_id'])->find();
                $v['type_info'] = $Config->info($v['type']);
                $v['car'] = $carModel->where(['driver_id'=>$v['id']])->select();
                if($param['lang']=='zh'){
                    if($v['state']==0){ $v['state_name'] = '请选择'; }
                    if($v['state']==1){ $v['state_name'] = '工作中'; }
                    if($v['state']==2){ $v['state_name'] = '空闲'; }
                    if($v['state']==3){ $v['state_name'] = '停工'; }
                }else{
                    if($v['state']==0){ $v['state_name'] = '请选择'; }
                    if($v['state']==1){ $v['state_name'] = '工作中'; }
                    if($v['state']==2){ $v['state_name'] = '空闲';}
                    if($v['state']==3){ $v['state_name'] = '停工'; }
                }
            }
            return $lists;
        }catch (\Exception $e){
            return [];
        }
    }

    /**
     * @param $param
     * @return int
     * @notes 修改员工状态
     * @author: dongdong
     * @date: 2025/1/18 14:18
     */
    public function staff_state($param){
        try {
            self::where(['id'=>$param['id']])->update(['state'=>$param]['state']);
            return 1;
        }catch (\Exception $e){
            return 0;
        }
    }


    /**
     * @param $data
     * @return int
     * @notes 员工添加
     * @author: dongdong
     * @date: 2025/1/18 14:32
     */
    public function staff_add($data){
        Db::startTrans();
        try {
            $title = $data['title'];
            $Attachment = new AttachmentModel();
            $RemarkModel = new RemarkModel();
            $title_arr = $RemarkModel ->translationAction($title);
            $attachment_id = '';
            $param['title'] = $title_arr['zh'];
            $param['title_ru'] = $title_arr['ru'];
            $param['phone'] = $data['phone'];
            $param['password'] = $data['password'];
            $param['create_at'] = date('Y-m-d H:i:s');
            $param['user_id'] = $data['user_id'];

            $param['type'] = $data['type'];
            $param['state'] = $data['state'];
            $param['id'] = $data['id']??0;

            $param['user_password'] = md5(md5($param['password']) . '6i7DS5');
            $param['salt'] = '6i7DS5';

            $insertGetId = 0;

            if($param['id']==0){
                $find_row = self::where(['phone'=>$data['phone']])->find();

                $row = Db::connect("fast_mysql")->table("fa_user")->where(['mobile'=>$data['phone']])->field("id")->find();
                if($row){
                    return '该手机号已注册';
                }

                if(empty($find_row)){
                    $param['bind_user_id'] = $data['user_id'];

                    $insertGetId = self::insertGetId($param);
                    if($insertGetId){
                        if(!empty($data['image'])&&(isset($data['image']))){
                            $arr = explode(',',$data['image'])??[];
                            foreach ($arr as $k =>&$v){
                                $attachment_id .= $Attachment->insertGetId(['user_id'=>$param['user_id'],'table_name'=>$this->table,'table_id'=>$insertGetId,'path'=>$v]).',';
                            }
                            $attachment_id = trim($attachment_id,',');
                            self::where(["id"=>$insertGetId])->update(['attachment_id'=>$attachment_id]);
                        }
                    }
                }
            }else{
                self::where(['id'=>$param['id']])->update($param);
                if(!empty($data['image'])&&(isset($data['image']))){
                    $arr = explode(',',$data['image'])??[];
                    foreach ($arr as $k =>&$v){
                        $attachment_id .= $Attachment->insertGetId(['user_id'=>$param['user_id'],'table_name'=>$this->table,'table_id'=>$param['id'],'path'=>$v]).',';
                    }
                    $attachment_id = trim($attachment_id,',');
                    self::where(["id"=>$param['id']])->update(['attachment_id'=>$attachment_id]);
                }
            }


            $d['username'] = $title_arr['zh'];
            $d['username_ru'] = $title_arr['ru'];
            if($data['lang']=='zh'){
                $d['nickname'] = $d['username'];
            }else{
                $d['nickname'] = $d['username_ru'];
            }
            $d['salt'] = '6i7DS5';
            $d['user_password'] = $param['password'];
            $d['password'] = md5(md5($param['password']) . '6i7DS5');
            $d['status'] = 'normal';
            $d['mobile'] = $data['phone'];
            $d['createtime'] = time();
            $d['updatetime'] = time();
            $find = Db::connect("fast_mysql")->table("fa_user")->where(['mobile'=>$data['phone']])->field("id")->find();
            if(empty($find)){
                $insert_id = Db::connect("fast_mysql")->table("fa_user")->insertGetId($d);
                if($insert_id){
                    Db::connect("fast_mysql")->table("fa_user_info")->insert(['user_id'=>$insert_id,'identity_id'=>12,'createtime'=>time(),'updatetime'=>time()]);
                }
                if($param['id']==0) {
                    self::where(["id" => $insertGetId])->update(['register_id' => $insert_id]);
                }else{
                    self::where(["id"=>$param['id']])->update(['register_id'=>$insert_id]);
                }
            }else{
                Db::connect("fast_mysql")->table("fa_user")->where(['mobile'=>$data['phone']])->update([
                    'user_password'=>$param['password'],
                    'password'=>$d['password'],
                    'username'=>$d['username'],
                    'username_ru' => $d['username_ru'],
                    'nickname' => $d['nickname']
                ]);
            }

            Db::commit();
            return 1;
        }catch (\Exception $e){
            Db::rollback();
            return 0;
        }
    }

    /**
     * @param $param
     * @return int|string
     * @notes 删除运输公司下的司机
     * @author: dongdong
     * @date: 2025/1/11 14:21
     */
    public function delete_driver($param)
    {
        try {
            self::where(['bind_user_id' => $param['bind_user_id'], 'register_id' => $param['user_id']])->update(['is_deleted' => 1]);
            return 1;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

    /**
     * 身份变更增加老板
     */
    public function insert_boss($user_id){
        $member = Db::connect("fast_mysql")->table("fa_user")->where(['id'=>$user_id])->field("id,password,salt,user_password,mobile,username,username_ru")->find();

        $d['user_id'] = $user_id;
        $d['register_id'] = $user_id;
        $d['bind_user_id'] = $user_id;

        $d['title'] = $member['username'];
        $d['title_ru'] = $member['username_ru'];
        $d['create_at'] = date('Y-m-d H:i:s');
        $d['phone'] = $member['mobile'];
        $d['type'] = 31;
        $d['password'] = $member['user_password'];
        $d['state'] = 1;
        $d['user_password'] = $member['password'];
        $d['salt'] = $member['salt'];

       return self::insert($d);
    }

}